home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / vt100 / window.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  11KB  |  415 lines

  1. /****************************************************
  2.  * vt100 emulator - window/keyboard support
  3.  *
  4.  *    v2.6 870227 DBW - bug fixes for all the stuff in v2.5
  5.  *    v2.5 870214 DBW - more additions (see readme file)
  6.  *    v2.4 861214 DBW - lots of fixes/additions (see readme file)
  7.  *    v2.3 861101 DBW - minor bug fixes
  8.  *    v2.2 861012 DBW - more of the same
  9.  *    v2.1 860915 DBW - new features (see README)
  10.  *         860823 DBW - Integrated and rewrote lots of code
  11.  *    v2.0 860809 DBW - Major rewrite
  12.  *    v1.1 860720 DBW    - Switches, 80 cols, colors, bug fixes
  13.  *    v1.0 860712 DBW    - First version released
  14.  *
  15.  ****************************************************/
  16.  
  17. #include "vt100.h"
  18.  
  19. /* keyboard definitions for toasc() */
  20. static char keys[75] = {
  21.     '`','1','2','3','4','5','6','7','8','9','0','-' ,
  22.     '=','\\', 0, '0','q','w','e','r','t','y','u','i','o' ,
  23.     'p','[',']', 0, '1','2','3','a','s','d','f','g','h' ,
  24.     'j','k','l',';','\'', 0, 0, '4','5','6', 0, 'z','x','c','v',
  25.     'b','n','m',44,'.','/', 0, '.','7','8','9',' ',8,
  26.     '\t',13,13,27,127,0,0,0,'-' } ;
  27.  
  28. /***************************************************
  29.  *  function to swap the use of backspace and delete
  30.  ***************************************************/
  31.  
  32. void swap_bs_del()
  33.     {
  34.     if (p_bs_del)   p_bs_del = 0;
  35.     else        p_bs_del = 1;
  36.  
  37.     keys[0x41] =    p_bs_del ? 127 : 8;
  38.     keys[0x46] =    p_bs_del ? 8 : 127;
  39.     }
  40.  
  41. /*************************************************
  42.  *  function to get file name (via a requestor)
  43.  *************************************************/
  44. void req(prmpt,name,getinp)
  45. char *prmpt,*name;
  46. int  getinp;
  47.     {
  48.     ULONG class;
  49.     unsigned int code, qual;
  50.     struct IntuiMessage *Msg;
  51.  
  52.     /* Make sure the prompt gets updated */
  53.     if (numreqs == 1 && strcmp(Prompt,prmpt) != 0) {
  54.     EndRequest(&myrequest,mywindow);
  55.     numreqs = 0;
  56.     }
  57.  
  58.     /* copy in a prompt and a default */
  59.     strcpy(Prompt,prmpt);
  60.     strcpy(InpBuf,name);
  61.  
  62.     /* If there is a requester... reuse it */
  63.     if (numreqs == 1) RefreshGadgets(&mystrgad,mywindow,&myrequest);
  64.  
  65.     /* otherwise create it */
  66.     else {
  67.     if (Request(&myrequest,mywindow) == 0) {
  68.         emits("ERROR - CAN'T CREATE REQUESTOR FOR:\n");
  69.         emits(Prompt); emit('\n');
  70.         emits(InpBuf); emit('\n');
  71.         return;
  72.         }
  73.     else numreqs = 1;
  74.     }
  75.  
  76.     /* if we don't want input, we're done */
  77.     if (getinp == 0 || numreqs == 0) return;
  78.  
  79.     /* throw away any extra messages */
  80.     Wait(1L << mywindow->UserPort->mp_SigBit);
  81.     while (Msg = (struct IntuiMessage *)GetMsg(mywindow->UserPort))
  82.     ReplyMsg(Msg);
  83.  
  84.     /* here is where we pre-select the gadget   */
  85.     if (!ActivateGadget(&mystrgad,mywindow,&myrequest)) {
  86.  
  87.     /* wait for his/her hands to get off the keyboard (Amiga-key) */
  88.     Delay(20L);
  89.     while (Msg = (struct IntuiMessage *)GetMsg(mywindow->UserPort))
  90.         ReplyMsg(Msg);
  91.  
  92.     /* try once more before giving up... */
  93.     ActivateGadget(&mystrgad,mywindow,&myrequest);
  94.     }
  95.  
  96.     /* wait for input to show up */
  97.     while (1) {
  98.     if ((NewMessage = (struct IntuiMessage *)
  99.         GetMsg(mywindow->UserPort)) == FALSE) {
  100.         Wait(1L<<mywindow->UserPort->mp_SigBit);
  101.         continue;
  102.         }
  103.     class = NewMessage->Class;
  104.     code  = NewMessage->Code;
  105.     qual  = NewMessage->Qualifier;
  106.     ReplyMsg(NewMessage);
  107.  
  108.     /* the requestor got terminated... yea!! */
  109.     if (class == REQCLEAR) break;
  110.  
  111.     /* maybe this is a menu item to handle */
  112.     if (class == MENUPICK) handle_menupick(class,code);
  113.     }
  114.  
  115.     /* all done, so return the result */
  116.     numreqs = 0;
  117.     strcpy(name,InpBuf);
  118.     }
  119.  
  120. /*************************************************
  121. *  function to print a string
  122. *************************************************/
  123. void emits(string)
  124. char string[];
  125.     {
  126.     int i;
  127.     char c;
  128.  
  129.     i=0;
  130.     while (string[i] != 0)
  131.     {
  132.     c=string[i];
  133.     if (c == 10) emit(13);
  134.     emit(c);
  135.     i += 1;
  136.     }
  137.     }
  138.  
  139. /*************************************************
  140. *  function to output ascii chars to window
  141. *************************************************/
  142. void emit(c)
  143. char c;
  144.     {
  145.     static char wrap_flag = 0;    /* are we at column 80? */
  146.  
  147.     c &= 0x7F;
  148.     switch( c )
  149.     {
  150.     case '\t':
  151.     x += 64 - ((x-MINX) % 64);
  152.     break;
  153.  
  154.     case 10:  /* lf */
  155.     y += 8;
  156.     break;
  157.  
  158.     case 13:  /* cr */
  159.     x = MINX;
  160.     break;
  161.  
  162.     case 8:   /* backspace */
  163.     x -= 8;
  164.     if (x < MINX) x = MINX;
  165.     break;
  166.  
  167.     case 12:     /* page */
  168.     x = MINX;
  169.     y = MINY;
  170.     SetAPen(mywindow->RPort,0L);
  171.     RectFill(mywindow->RPort,(long)MINX,
  172.         (long)(MINY-7),(long)(MAXX+7),(long)(MAXY+1));
  173.     SetAPen(mywindow->RPort,1L);
  174.     break;
  175.  
  176.     case 7:     /* bell */
  177.     if (p_volume == 0) DisplayBeep(NULL);
  178.     else {
  179.         BeginIO(&Audio_Request);
  180.         WaitIO(&Audio_Request);
  181.         }
  182.     break;
  183.  
  184.     default:
  185.     if (c < ' ' || c > '~') break;
  186.     if (p_wrap && wrap_flag && x >= MAXX) {
  187.         x = MINX;
  188.         y += 8;
  189.         if (y > MAXY) {
  190.         y = MAXY;
  191.         ScrollRaster(mywindow->RPort,0L,8L,(long)MINX,
  192.             (long)(MINY-6),(long)(MAXX+7),(long)(MAXY+1));
  193.         }
  194.         }
  195.     Move(mywindow->RPort,(long)x,(long)y);
  196.  
  197.     if (curmode&FSF_BOLD) {
  198.         if (p_depth > 1) {
  199.         SetAPen(mywindow->RPort,(long)(2+(1^p_screen)));
  200.         SetSoftStyle(mywindow->RPort,(long)curmode,253L);
  201.         }
  202.         else SetSoftStyle(mywindow->RPort,(long)curmode,255L);
  203.         }
  204.     else SetSoftStyle(mywindow->RPort,(long)curmode,255L);
  205.  
  206.     if (curmode&FSF_REVERSE) {
  207.         SetDrMd(mywindow->RPort,(long)(JAM2+INVERSVID));
  208.         Text(mywindow->RPort,&c,1L);
  209.         SetDrMd(mywindow->RPort,(long)JAM2);
  210.         }
  211.     else Text(mywindow->RPort,&c,1L);
  212.  
  213.     if (curmode&FSF_BOLD) SetAPen(mywindow->RPort,1L);
  214.     x += 8;
  215.     } /* end of switch */
  216.  
  217.     if (y > MAXY) {
  218.     y = MAXY;
  219.     x = MINX;
  220.     ScrollRaster(mywindow->RPort,0L,8L,(long)MINX,
  221.         (long)(MINY-6),(long)(MAXX+7),(long)(MAXY+1));
  222.     }
  223.     if (x > MAXX) {
  224.     wrap_flag = 1;
  225.     x = MAXX;
  226.     }
  227.     else wrap_flag = 0;
  228.     }
  229.  
  230. /*************************************************
  231. *  function to output ascii chars to window (batched)
  232. *************************************************/
  233. void emitbatch(la,lookahead)
  234. int la;
  235. char *lookahead;
  236.     {
  237.     int i;    
  238.  
  239.     Move(mywindow->RPort,(long)x,(long)y);
  240.     i = x / 8;
  241.     if (i+la >= maxcol) {
  242.     if (p_wrap == 0) la = maxcol - i;
  243.     else {
  244.         lookahead[la] = 0;
  245.         emits(lookahead);
  246.         return;
  247.         }
  248.     }
  249.     if (curmode&FSF_BOLD) {
  250.     if (p_depth > 1) {
  251.         SetAPen(mywindow->RPort,(long)(2+(1^p_screen)));
  252.         SetSoftStyle(mywindow->RPort,(long)curmode,253L);
  253.         }
  254.     else SetSoftStyle(mywindow->RPort,(long)curmode,255L);
  255.     }
  256.     else SetSoftStyle(mywindow->RPort,(long)curmode,255L);
  257.  
  258.     if (curmode&FSF_REVERSE) {
  259.     SetDrMd(mywindow->RPort,(long)(JAM2+INVERSVID));
  260.     Text(mywindow->RPort,lookahead,(long)la);
  261.     SetDrMd(mywindow->RPort,(long)JAM2);
  262.     }
  263.     else Text(mywindow->RPort,lookahead,(long)la);
  264.     if (curmode&FSF_BOLD) SetAPen(mywindow->RPort,1L);
  265.     x += (8 * la);
  266.     }
  267.  
  268. /******************************
  269. * Manipulate cursor
  270. ******************************/
  271. void cursorflip()
  272.     {
  273.     SetDrMd(mywindow->RPort,(long)COMPLEMENT);
  274.     SetAPen(mywindow->RPort,3L);
  275.     RectFill(mywindow->RPort,
  276.     (long)(x-1),(long)(y-6),(long)(x+8),(long)(y+1));
  277.     SetAPen(mywindow->RPort,1L);
  278.     SetDrMd(mywindow->RPort,(long)JAM2);
  279.     }
  280.  
  281. /************************************************
  282. *  function to take raw key data and convert it 
  283. *  into ascii chars
  284. **************************************************/
  285. int toasc(code,qual,local)
  286. unsigned int code,qual;
  287. int local;
  288.     {
  289.     unsigned int ctrl,shift,capsl,amiga,alt;
  290.     char c = 0, keypad = 0;
  291.     char *ptr;
  292.  
  293.     ctrl    = qual & IEQUALIFIER_CONTROL;
  294.     capsl   = qual & IEQUALIFIER_CAPSLOCK;
  295.     amiga   = qual & (IEQUALIFIER_LCOMMAND | IEQUALIFIER_RCOMMAND);
  296.     shift   = qual & (IEQUALIFIER_LSHIFT   | IEQUALIFIER_RSHIFT);
  297.     alt        = qual & (IEQUALIFIER_LALT     | IEQUALIFIER_RALT);
  298.  
  299.     switch ( code )
  300.     {
  301.     case 98:
  302.     case 226:
  303.     case 99:
  304.     case 227:
  305.     case 96:
  306.     case 97:
  307.     case 224:
  308.     case 225:
  309.     case 100:
  310.     case 101:
  311.     case 228:
  312.         case 229:
  313.     case 102:
  314.     case 103:
  315.     case 230:
  316.     case 231:   c = 0; break; /* ctrl, shift, capsl, amiga, or alt */
  317.  
  318.     case 0x50: 
  319.     case 0x51: 
  320.     case 0x52: 
  321.     case 0x53: 
  322.     case 0x54: 
  323.     case 0x55: 
  324.     case 0x56: 
  325.     case 0x57: 
  326.     case 0x58: 
  327.     case 0x59:  c = 0;
  328.             if (shift)    ptr = p_F[code - 0x50];
  329.             else    ptr = p_f[code - 0x50];
  330.             if (!script_on && *ptr == p_keyscript)
  331.                 script_start(++ptr);
  332.             else    sendstring(ptr);
  333.             break;
  334.     case 0x0f: c = (p_keyapp) ? 'p' : '0'; keypad = TRUE; break;
  335.     case 0x1d: c = (p_keyapp) ? 'q' : '1'; keypad = TRUE; break;
  336.     case 0x1e: c = (p_keyapp) ? 'r' : '2'; keypad = TRUE; break;
  337.     case 0x1f: c = (p_keyapp) ? 's' : '3'; keypad = TRUE; break;
  338.     case 0x2d: c = (p_keyapp) ? 't' : '4'; keypad = TRUE; break;
  339.     case 0x2e: c = (p_keyapp) ? 'u' : '5'; keypad = TRUE; break;
  340.     case 0x2f: c = (p_keyapp) ? 'v' : '6'; keypad = TRUE; break;
  341.     case 0x3d: c = (p_keyapp) ? 'w' : '7'; keypad = TRUE; break;
  342.     case 0x3e: c = (p_keyapp) ? 'x' : '8'; keypad = TRUE; break;
  343.     case 0x3f: c = (p_keyapp) ? 'y' : '9'; keypad = TRUE; break;
  344.     case 0x43: c = (p_keyapp) ? 'M' : 13 ; keypad = TRUE; break;
  345.     case 0x4a: c = (p_keyapp) ? 'l' : '-'; keypad = TRUE; break;
  346.     case 0x5f: sendstring("\033Om") ;break;
  347.     case 0x3c: c = (p_keyapp) ? 'n' : '.'; keypad = TRUE; break;
  348.     case 0x4c:
  349.     case 0x4d: 
  350.     case 0x4e: 
  351.     case 0x4f: sendchar(27);            /* cursor keys */
  352.            if (p_curapp) sendchar('O');
  353.            else sendchar('[');
  354.            sendchar(code - 11);
  355.            break;
  356.  
  357.     default:
  358.     if (code < 75) c = keys[code];
  359.     else c = 0;
  360.     }
  361.  
  362.     if (keypad) {
  363.         if (p_keyapp) sendstring("\033O");
  364.         sendchar(c);
  365.         return(0);
  366.     }
  367.         
  368.     /* add modifiers to the keys */
  369.  
  370.     if (c != 0) {
  371.     if (shift) {
  372.         if ((c <= 'z') && (c >= 'a')) c -= 32;
  373.         else
  374.         switch( c ) {
  375.         case '[':  c = '{'; break;
  376.         case ']':  c = '}'; break;
  377.         case '\\': c = '|'; break;
  378.         case '\'': c = '"'; break;
  379.         case ';':  c = ':'; break;
  380.         case '/':  c = '?'; break;
  381.         case '.':  c = '>'; break;
  382.         case ',':  c = '<'; break;
  383.         case '`':  c = '~'; break;
  384.         case '=':  c = '+'; break;
  385.         case '-':  c = '_'; break;
  386.         case '1':  c = '!'; break;
  387.         case '2':  c = '@'; break;
  388.         case '3':  c = '#'; break;
  389.         case '4':  c = '$'; break;
  390.         case '5':  c = '%'; break;
  391.         case '6':  c = '^'; break;
  392.         case '7':  c = '&'; break;
  393.         case '8':  c = '*'; break;
  394.         case '9':  c = '('; break;
  395.         case '0':  c = ')'; break;
  396.         default:            break;
  397.         }
  398.         }
  399.     else if (capsl && (c <= 'z') && (c >= 'a')) c -= 32;
  400.     }
  401.     if (ctrl) {
  402.     if (c > '`' && c <= 127) c -= 96;
  403.     else if (c > '@' && c <= '_') c -= 64;
  404.     else if (c == '6') c = 30;
  405.     else if (c == '-' || c == '?') c = 31;
  406.     }
  407.     if (ctrl && (c == '@' || c == '2' || c == ' ')) {
  408.     if (!local) sendchar(alt?128:0);
  409.     c = 0;
  410.     }
  411.     else if (c != 0 && (!local)) sendchar(alt?c+128:c);
  412.     return((int)c);
  413.     }
  414.  
  415.